package com.atjava.nebula.core.util.func;

import sun.misc.BASE64Decoder;

/**字符串相关的辅助处理
 * @author kyo
 *
 */
public class StringUtil {
	
	/**判断是否是UTF-8编码
	 * @param b 要判断的字符内容，如String.getByte("ISO-8859-1")
	 * @param aMaxCount 要用于判断的字符长度
	 * @return 如果是UTF－8编码则返回true
	 */
	public static boolean isValidUtf8(byte[] b,int aMaxCount){ 

	       int lLen=b.length,lCharCount=0; 

	       for(int i=0;i<lLen && lCharCount<aMaxCount;++lCharCount){ 

	              byte lByte=b[i++];//to fast operation, ++ now, ready for the following for(;;) 

	              if(lByte>=0) continue;//>=0 is normal ascii 

	              if(lByte<(byte)0xc0 || lByte>(byte)0xfd) return false; 

	              int lCount=lByte>(byte)0xfc?5:lByte>(byte)0xf8?4 

	                     :lByte>(byte)0xf0?3:lByte>(byte)0xe0?2:1; 

	              if(i+lCount>lLen) return false; 

	              for(int j=0;j<lCount;++j,++i) if(b[i]>=(byte)0xc0) return false; 

	       } 

	       return true; 

	} 
	
	 /**去除字符串的左边空格
	 * @param str
	 * @return
	 */
	public static String leftTrim(String str) {
		if (str == null || str.equals("")) {
			return str;
		} else {
			return str.replaceAll("^[　 ]+", "");
		}
	}

	/**去除字符串的右边空格
	 * @param str
	 * @return
	 */
	public static String rightTrim(String str) {
		if (str == null || str.equals("")) {
			return str;
		} else {
			return str.replaceAll("[　 ]+$", "");
		}
	}	
	/**将 BASE64 编码的字符串 s 进行解码 
	 * @param s
	 * @return
	 */
	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private static final char SEPARATOR = '_';

    public static String toUnderlineName(String s) {
        if (s == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i >= 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    if (i > 0) sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString().toUpperCase();
    }

    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }

        s = s.toLowerCase();

        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static String toCapitalizeCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = toCamelCase(s);
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

    public static void main(String[] args) {
        System.out.println(toUnderlineName("ISOCertifiedStaff"));
        System.out.println(toUnderlineName("ddd"));
        System.out.println(toUnderlineName("UserID"));
        System.out.println(toCamelCase("iso_certified_staff"));
        System.out.println(toCamelCase("certified_staff"));
        System.out.println(toCamelCase("userId"));
    }
}
